package com.hjw.weixun.db;


import java.io.Serializable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * 
 * @author henry_chen
 * date 2011-8-2
 * jdbc的工具类
 *  主要为释放资源用
 */
public class JdbcUtil implements Serializable {
	
	private static final long serialVersionUID = -1044969070540750162L;
	
	private static  final  Logger logger = LoggerFactory.getLogger(JdbcUtil.class);
	private static JdbcUtil util = null;
	private static DataSource dataSource = null;

	public static void main(String[] args) {
	}

	public static JdbcUtil getInstance() {
		if (util == null) {
			synchronized (JdbcUtil.class) {
				util = new JdbcUtil();
			}
		}
		return util;
	}
	
	private JdbcUtil(){
		dataSource = new DataSource();
	}

	// 获得连接对象
	public Connection getConnection() {
		return dataSource.getConnection();
	}

	// 释放资源
	public static void free(ResultSet rs, Statement sta, Connection conn) {
		
		try {
			if (rs != null)
				rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (sta != null)
					sta.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				if (conn != null)
					dataSource.freeConnection(conn);
			}
		}
		
		
		
		
//		if (rs != null) {
//			try {
//				rs.close();
//			} catch (SQLException e) {
//				e.printStackTrace();
//			} finally {
//				if (sta != null) {
//					try {
//						sta.close();
//					} catch (SQLException e) {
//						e.printStackTrace();
//					} finally {
//						if (conn != null) {
//							//conn.close();
//							dataSource.freeConnection(conn);
//						}
//					}
//				}
//			}
//		}
	}

}
